const app = getApp(); 
Page({
  data: {
    tabbar: {},
    grids:[],
    userInfo:{},
    certification:'去认证',
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    login: false,
    dottot:0
  },
   showModal(e) {
    this.setData({
        modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  onLoad: function (options){
    app.changeTabBar();
  },
  certification:function(){
    wx.navigateTo({
      url: '../certificationStep/certificationStep'
    })
  },
  personal:function(){
    wx.navigateTo({
      url: '../selfMessage/selfMessage'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  bindGetUserInfo:function (e){
    const that = this;
    wx.getSetting({
      success(res){
        if (res.authSetting['scope.userInfo']){
          wx.getUserInfo({
            success(a) {
              var str = JSON.parse(a.rawData);
              wx.login({
                success(res) {
                  if (res.code) {
                    app.util.request({
                      url: 'Index/Index/getOpenid',
                      data: {
                        avatarUrl: str.avatarUrl,
                        city: str.city,
                        country: str.country,
                        gender: str.gender,
                        language: str.language,
                        nickName: str.nickName,
                        province: str.province,
                        code: res.code
                      },
                      header: {
                        'content-type': 'application/json'
                      },
                      success(res){
                        app.globalData.userInfo = res.data;
                        that.setData({
                          userInfo: res.data
                        })
                        app.util.request({
                          url: 'Index/Index/certificationSchedule',
                          data: {
                            openid: res.data.openid,
                          },
                          header: {
                            'content-type': 'application/json'
                          },
                          success(res) {
                            console.log(res)
                            that.setData({
                              modalName:null
                            })
                            if (res.data === null || res.data.length == 0) {
                              console.log('暂无数据');
                            } else {
                              if (res.data.is_type === '已通过') {
                                that.setData({
                                  certification: '已认证',
                                })
                              }
                            }
                          }
                        })
                      }
                    })
                  } else {
                    console.log('登录失败！' + res.errMsg)
                  }
                }
              })
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function(){
    const that = this;
    // 检验是否授权
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          that.setData({
            login: true,
            modalName: 'Image'
          })
        } else {
          that.setData({
            login: false
          })
        }
      }
    })
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success(res) {
              var str = JSON.parse(res.rawData);
              wx.login({
                success(res) {
                  if (res.code) {
                    app.util.request({
                      url: 'Index/Index/getOpenid',
                      data: {
                        avatarUrl: str.avatarUrl,
                        city: str.city,
                        country: str.country,
                        gender: str.gender,
                        language: str.language,
                        nickName: str.nickName,
                        province: str.province,
                        code: res.code
                      },
                      header: {
                        'content-type': 'application/json'
                      },
                      success(res) {
                        that.setData({
                          userInfo: res.data
                        })
                        app.globalData.userInfo = res.data;
                        that.setData({
                          userInfo: res.data
                        })
                        app.util.request({
                          url: 'Index/Index/certificationSchedule',
                          data: {
                            openid: app.globalData.userInfo.openid,
                          },
                          header: {
                            'content-type': 'application/json'
                          },
                          success(res) {
                            if (res.data === null || res.data.length == 0) {
                              console.log('暂无数据');
                            } else {
                              if (res.data.is_type === '已通过') {
                                that.setData({
                                  certification: '已认证',
                                })
                              }
                            }
                          }
                        })
                      }
                    })
                  }else{
                    console.log('登录失败！' + res.errMsg)
                  }
                }
              })
            }
          })
        }else{
          // wx.showToast({
          //   title: '请点击头像获取授权信息',
          //   icon: 'none',
          //   duration: 2000
          // });
          // return false;
        }
      }
    })
    app.util.request({
      url: 'Index/Index/MineList',
      header: {
        'content-type': 'application/json'
      },
      success(res){
        if (res.data === undefined || res.data.length == 0) {
          console.log('暂无数据');
        }else{
          if (app.globalData.userInfo.length === undefined){
            if (that.data.userInfo.length === undefined){
              that.setData({
                grids: res.data
              })
            }else{
              that.setData({
                userInfo: that.data.userInfo,
                grids: res.data
              })
            }
          }else{
            that.setData({
              userInfo: app.globalData.userInfo,
              grids: res.data
            })
          }
        }
      }
    })
    app.util.request({
      url: 'Index/Index/ButtonNavList',
      header: {
        'content-type': 'application/json'
      },
      success(res){
        var data = res.data;
        for (var i = 0; i < data.length; i++){
          data[i]['index'] = i;
          data[i]['pagePath'] = data[i]['path'];
          data[i]['text'] = data[i]['name'];
          data[i]['iconPath'] = data[i]['before_image'];
          data[i]['selectedIconPath'] = data[i]['after_image'];
          data[i]['selected'] = false;
        }
        app.util.request({
          url: 'Index/Index/getSumMessage',
          data: {
            openid: app.globalData.userInfo.openid,
          },
          header: {
            'content-type': 'application/json'
          },
          success(res) {
            data[2]['dott'] = res.data;
            data[0]['selected'] = true;
            app.globalData.tabbar.list = data;
            app.setchangeTabBar();
            that.setData({
              dottot: data[2]['dott']
            })
          }
        })
        data[0]['selected'] = true;
        app.globalData.tabbar.list = res.data;
        app.setchangeTabBar();
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})